﻿using System;
using System.Collections.Generic;
using UnityEngine;

# if UNITY_EDITOR
using UnityEditor;
#endif

namespace HKTools
{
    /// <summary>
    /// HK_TaskSystem 任务流数据结构
    /// </summary>
    public class HK_TaskList : ScriptableObject
    {
        [SerializeField]
        public List<HK_Task> list;

        [SerializeField, HideInInspector]
        List<string> conditionCache = new List<string>();

        [SerializeField, HideInInspector]
        List<string> taskCache = new List<string>();

        void OnValidate()
        {
            bool isChangeTask = false;
            bool isChangeCondition = false;

            if (taskCache.Count != Enum.GetValues(typeof(HK_TaskType)).Length)
            {
                isChangeTask = true;
                Debug.Log("TaskName 枚举 Count 变更");
            }

            // Condition 枚举 数量变更
            if (conditionCache.Count != Enum.GetValues(typeof(HK_ConditionType)).Length)
            {
                isChangeCondition = true;
                Debug.Log("Condition 枚举 Count 变更");
            }

            // TaskName 枚举 排序变更
            if (!isChangeTask)
            {
                int changeNum = 0;

                for (int i = 0; i < Enum.GetValues(typeof(HK_TaskType)).Length; i++)
                {
                    if (taskCache[i] != Enum.GetNames(typeof(HK_TaskType))[i])
                    {
                        changeNum++;
                    }
                }

                if (changeNum >= 2)
                {
                    isChangeTask = true;
                    Debug.Log("TaskName 枚举 Sort 变更");
                }
            }

            // Condition 枚举 排序变更
            if (!isChangeCondition)
            {
                int changeNum = 0;

                for (int i = 0; i < Enum.GetNames(typeof(HK_ConditionType)).Length; i++)
                {
                    if (conditionCache[i] != Enum.GetNames(typeof(HK_ConditionType))[i])
                    {
                        changeNum++;
                    }
                }

                if (changeNum >= 2)
                {
                    isChangeCondition = true;
                    Debug.Log("Condition 枚举 Sort 变更");
                }
            }

            foreach (var task in list)
            {
                if (isChangeTask)
                {
                    try
                    {
                        task.task = (HK_TaskType)Enum.Parse(typeof(HK_TaskType), task.taskName);
                    }
                    catch (ArgumentException exception)
                    {
                        Debug.LogError(exception.Message);
                    }
                }

                task.taskName = task.task.ToString();

                for (int i = 0; i < task.conditions.Count; i++)
                {
                    if (isChangeCondition)
                    {
                        try
                        {
                            task.conditions[i].condition = (HK_ConditionType)Enum.Parse(typeof(HK_ConditionType), task.conditions[i].conditionName);
                        }
                        catch (ArgumentException exception)
                        {
                            Debug.LogError("别一块操作...TaskName: " + task.task + " Condition没有变更过来，手动修改吧！" + exception.Message);
                        }
                    }

                    task.conditions[i].conditionName = task.conditions[i].condition.ToString();
                }
            }

            conditionCache.Clear();
            foreach (string name in Enum.GetNames(typeof(HK_ConditionType)))
            {
                conditionCache.Add(name);
            }

            taskCache.Clear();
            foreach (string name in Enum.GetNames(typeof(HK_TaskType)))
            {
                taskCache.Add(name);
            }

#if UNITY_EDITOR
            EditorUtility.SetDirty(this);
#endif
        }

        void OnDisable()
        {
            // 对 ScriptableObject 文件进行初始化
            foreach (var task in list)
            {
                task.isOn = false;
                task.isFinished = false;

                for (int i = 0; i < task.conditions.Count; i++)
                {
                    task.conditions[i].isFinish = false;
                }
            }
        }
    }

    [Serializable]
    public class HK_TaskCondition
    {
        [HK_ReadOnly, HideInInspector]
        public string conditionName;

        //[HK_EnumRestrict(5, 15)]
        public HK_ConditionType condition;

        public bool isFinish;
    }

    [Serializable]
    public class HK_Task
    {
        [HK_ReadOnly, HideInInspector]
        public string taskName;
        // 任务名称
        public HK_TaskType task;

        // 任务是否正在进行
        public bool isOn = false;

        // 任务是否完成
        public bool isFinished = false;

        // 任务描述
        public string description;

        // 任务完成的条件
        public List<HK_TaskCondition> conditions = new List<HK_TaskCondition>();

        // 任务开始讲解音频
        public AudioClip taskStartAudio;

        // 讲解音频播放的延时时间
        [Range(0, 10)]
        public float audioDelay;

        // 下一个任务的名称（用于非线性任务跳转）
        public HK_TaskType nextTast;

        // 下一个任务的延时
        [Range(0, 10)]
        public float nextTaskDelay;
    }
}